<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--通用查询-->
      <queryHeader :permission="permission" />
      <!--通用编辑-->
      <crudOperation :permission="permission" />
    </div>

    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <!--展示字段-->
      <el-table-column v-if="columns.visible('articleTitle')" min-width="160" prop="articleTitle" label="文章标题" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('articleAuthor')" prop="articleAuthor" label="文章作者" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('sections')" prop="sections" label="所在栏目" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('publishPublishStatus')&&crud.query.sectionId" prop="publishStatus" label="上架状态" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.status_article_publish[scope.row.publishPublishStatus] }}
        </template>
      </el-table-column>
      <el-table-column v-if="columns.visible('publishTime')&&crud.query.sectionId" prop="publishTime" label="发布时间" :show-overflow-tooltip="true" />
      <el-table-column v-if="columns.visible('isSticky')&&crud.query.sectionId" prop="isSticky" label="是否置顶" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ dict.label.status_common_yesno[scope.row.isSticky] }}
        </template>
      </el-table-column>
      <!--操作-->
      <el-table-column label="操作" width="180px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="preview(scope.row)">预览</el-button>
          <el-button v-if="crud.query.sectionId" size="mini" type="text" @click="publishChang(scope.row)">{{ scope.row.publishPublishStatus === 1? '下架':'上架' }}</el-button>
          <el-button v-if="scope.row.publishPublishStatus === 1 && crud.query.sectionId" size="mini" type="text" @click="stickyChang(scope.row)">{{ scope.row.isSticky === 0?'置顶':'取消置顶' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import crudArticlePublish, { optPublish, optSticky } from '@/api/business/common/article/article-publish'
import queryHeader from './module/queryHeader'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

// crud交由presenter持有
const crud = CRUD({ title: 'ArticlePublish', url: 'article/publish/bysection/list/page', crudMethod: { ...crudArticlePublish }, pkIdName: 'id' })

export default {
  name: 'ArticlePublish',
  components: { queryHeader, crudOperation, pagination },
  dicts: ['type_article', 'status_common_yesno', 'status_article_publish'],
  mixins: [presenter(crud)],
  data() {
    return {
      // 对应几个按钮操作
      permission: {
        add: [],
        edit: [],
        del: []
      }
    }
  },
  created() {
    this.crud.optShow = { add: false, edit: false, del: false, download: false }
  },
  methods: {
    preview(row) {
      this.$router.push({ path: 'preview', query: { id: row.id }})
    },
    publishChang(row) {
      optPublish(this.crud.query.sectionId, row.id, row.publishPublishStatus === 1 ? 2 : 1).then(() => {
        this.$notify({ title: '操作成功', type: 'success', duration: 2500 })
      })
    },
    stickyChang(row) {
      optSticky(this.crud.query.sectionId, row.id, row.isSticky === 0 ? 1 : 0).then(() => {
        this.$notify({ title: '操作成功', type: 'success', duration: 2500 })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
